package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojo.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author by jojo
 * @Date 2022/2/25
 * @Description
 */
@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {
    @Override
    public ResponseResult<List<AdChannel>> findByNameAndPage(ChannelDTO dto) {
        //检测参数
        if (dto==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //检验分页参数，为null就添加page=1，size=10
        dto.checkParam();
        Page<AdChannel> page = new Page<>(dto.getPage(), dto.getSize());
        //按名称模糊查询
        LambdaQueryWrapper<AdChannel> lambdaQuery = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(dto.getName())) {
            lambdaQuery.like(AdChannel::getName,dto.getName());
        }
        if (dto.getStatus()!=null) {
            lambdaQuery.eq(AdChannel::getStatus,dto.getStatus());
        }
        //添加排序
        lambdaQuery.orderByAsc(AdChannel::getOrd);
        //返回分页查询的对象
        IPage<AdChannel> result = this.page(page, lambdaQuery);
        //封装
        return  new PageResponseResult(dto.getPage(), dto.getSize(),
                result.getTotal(),result.getRecords());
    }

    @Override
    public ResponseResult addChannel(AdChannel channel) {
        //校验参数
        if (channel==null|| StringUtils.isBlank(channel.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //判断频道是否存在
        LambdaQueryWrapper<AdChannel> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(AdChannel::getName,channel.getName());
        int count=this.count(lambdaQuery);
        if (count>0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"该频道已存在");
        }
        //判断频道名称是否为空，长度是否大于10
        if (channel.getName().length()<=10) {
            channel.setCreatedTime(new Date());
            //执行新增
            save(channel);
            //返回响应
            return ResponseResult.okResult();
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.ADMING_CHANNEL_STATUS_TRUE,"频道名称大于10");
        }
    }

    @Override
    public ResponseResult updateChannel(AdChannel channel) {
        //校验参数
        if (channel==null||channel.getId()==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //按id查询频道是否存在
        AdChannel adChannel = getById(channel.getId());
        if (adChannel==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"该频道不存在");
        }
        //校验名称的唯一性
        if (StringUtils.isNotBlank(channel.getName())
                &&!channel.getName().equals(adChannel.getName())){
            //判断频道是否存在
            LambdaQueryWrapper<AdChannel> lambdaQuery = Wrappers.lambdaQuery();
            lambdaQuery.eq(AdChannel::getName,channel.getName());
            int count=this.count(lambdaQuery);
            if (count>0) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"该频道已存在");
            }
        }
        updateById(channel);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteById(Long id) {
        //校验参数
        if (id==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断当前频道是否存在或是否有效
        AdChannel channel = getById(id);
        if (channel==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //启动状态下是不能删除的
        if (channel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }
        //删除频道
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
